(function(window){
    var u = {};

    buildSql = function (sql, params) {
        var ret = sql;
        if (params) {
            for (var i = 0; i < params.length; ++i) {
                var para = params[i];
    
                //安全检查
                if (typeof para == 'undefined') {
                    return {
                        "state": false,
                        "msg": '第' + i + "个参数是undefined"
                    }
                }
                if (typeof para == 'string') {
                    if (para) para = "'" + para + "'";
                    else para = 'null';
                }
                sql = sql.replace("?", para)
                if (sql == ret) {
                    return {
                        "state": false,
                        "msg": '参数个数多了'
                    }
                }
                ret = sql;
            }
    
            if (/\?/.test(sql)) return {
                "state": false,
                "msg": '参数个数少了'
            }
        }
        return {
            "state": true,
            "sql": ret
        }
    }
    
    var getObjectClass = function (obj) {
        if (obj && obj.constructor && obj.constructor.toString()) {
            /*
             * for browsers which have name property in the constructor
             * of the object,such as chrome 
             */
            if (obj.constructor.name) {
                return obj.constructor.name;
            }
            var str = obj.constructor.toString();
            /*
             * executed if the return of object.constructor.toString() is 
             * "[object objectClass]"
             */
            if (str.charAt(0) == '[') {
                var arr = str.match(/\[\w+\s*(\w+)\]/);
            } else {
                /*
                 * executed if the return of object.constructor.toString() is 
                 * "function objectClass () {}"
                 * for IE Firefox
                 */
                var arr = str.match(/function\s*(\w+)/);
            }
            if (arr && arr.length == 2) {
                return arr[1];
            }
        }
        return undefined;
    }
    
    function _buildInsertSql(obj, selectived) {
        if (typeof obj != "object") {
            return null;
        }
    
        var className = getObjectClass(obj);
        if (className == undefined) {
            return null;
        }
    
        var columnNames = [];
        var columnValues = [];
        for (var propertyName in obj) {
            if (selectived) {
                var value = obj[propertyName]
                if (value != undefined && value != null) {
                    columnNames.push(propertyName)
                    columnValues.push(obj[propertyName])
                }
            } else {
                columnNames.push(propertyName)
                columnValues.push(obj[propertyName])
            }
    
        }
    
        var sql = "insert into " + className + "("
    
        for (var i = 0; i < columnNames.length - 1; ++i) {
            sql += columnNames[i] + ",";
        }
        sql += columnNames[i] + ") values("
    
        for (var i = 0; i < columnNames.length - 1; ++i) {
            sql += "?, "
        }
        sql += "?)"
    
    
        var result = buildSql(sql, columnValues)
        if (result.state) {
            return result.sql;
        }
        return null;
    }
    
    function buildInsertSql(obj) {
        return _buildInsertSql(obj, false)
    }
    
    function buildInsertSelectiveSql(obj) {
        return  _buildInsertSql(obj, true)
    }

    


    window.ormUtil = u;
})(window)